<?php
namespace app\admin\controller;
use app\admin\logic\PatrolRecordLogic;

/**
 * 巡店明细api控制器
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/11/18
 * Time: 14:22
 */
class PatrolRecord extends Base {

    public function __construct(){
        parent::__construct();
        $this->pr_logic = new PatrolRecordLogic();
    }

    /**
     * 获取巡店明细
     * @return \think\response\Json
     */
    public function ajax_get_pr(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        $maps = $this->pr_logic->get_search_pr_maps($params['branchid'],$params['floorid'],$params['cp_type'],$params['username'],$params['start_time'],$params['end_time']);
        $limit = ($params['page'] - 1) * $params['rows'];

        $list = model('cp_floor')->get_list($maps,'id,openid,cp_type,branchid,floorid,end_time','end_time desc','',$limit,$params['rows']);
        foreach($list as $k => $v){
            $list[$k]['type_zw'] = $v->cp_type_name;

            //查找用户
            $u_maps = [];
            $u_maps[] = ['openid','=',$v['openid']];
            $u_detail = model('user')->get_detail($u_maps,'id,username');
            $list[$k]['username'] = $u_detail['username'];

            //查找楼层
            $f_maps = [];
            $f_maps[] = ['id','=',$v['floorid']];
            $f_detail = model('floor')->get_detail($f_maps,'id,name');
            $list[$k]['floor_zw'] = $f_detail['name'];

            //查找分部
            $b_maps = [];
            $b_maps[] = ['id','=',$v['branchid']];
            $b_detail = model('branch')->get_detail($b_maps,'id,name');
            $list[$k]['branch_zw'] = $b_detail['name'];
        }

        $total_num = model('cp_floor')->get_count($maps,'id');

        $data['list'] = $list;
        $data['total_num'] = $total_num;
        return json($this->pr_logic->get_response(1,'OK',$data));
    }

    /**
     * 获取巡店明细详情
     * @return \think\response\Json
     */
    public function ajax_get_pr_detail(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        $maps = [];
        $maps[] = ['id','=',$params['cfid']];
        $maps[] = ['status','=',1];
        $cf_detail = model('cp_floor')->get_detail($maps,'id,openid,cp_type,cp_branchid,branchid,floorid,end_time');
        $cf_detail['type_zw'] = $cf_detail->cp_type_name;

        //查找用户
        $u_maps = [];
        $u_maps[] = ['openid','=',$cf_detail['openid']];
        $u_detail = model('user')->get_detail($u_maps,'id,username');
        $cf_detail['username'] = $u_detail['username'];

        //查找楼层
        $f_maps = [];
        $f_maps[] = ['id','=',$cf_detail['floorid']];
        $f_detail = model('floor')->get_detail($f_maps,'id,name');
        $cf_detail['floor_zw'] = $f_detail['name'];

        //查找分部
        $b_maps = [];
        $b_maps[] = ['id','=',$cf_detail['branchid']];
        $b_detail = model('branch')->get_detail($b_maps,'id,name');
        $cf_detail['branch_zw'] = $b_detail['name'];

        //查找巡店信息
        $s = new \app\common\controller\Space();

        $maps = [];
        $maps[] = ['floorid','=',$cf_detail['floorid']];
        $r_list = model('region')->get_list($maps,'id,name,is_cp_ws');
        $list = [];
        foreach($r_list as $k_1 => $v_1){
            $list[$k_1] = $v_1->toArray();

            $maps = [];
            $maps[] = ['regionid','=',$v_1['id']];
            $m_list = model('modular')->get_list($maps,'id,name');
            $temp_list = [];
            foreach($m_list as $k_2 => $v_2){
                $temp_list[$k_2] = $v_2->toArray();

                $maps = [];
                $maps[] = ['cp_type','=',$cf_detail['cp_type']];
                $maps[] = ['cp_branchid','=',$cf_detail['cp_branchid']];
                $maps[] = ['cp_floorid','=',$cf_detail['id']];
                $maps[] = ['branchid','=',$cf_detail['branchid']];
                $maps[] = ['floorid','=',$cf_detail['floorid']];
                $maps[] = ['regionid','=',$v_1['id']];
                $maps[] = ['modularid','=',$v_2['id']];
                $crm_detail = model('cp_region_modular')->get_detail($maps);
                $crm_detail['action_zw'] = $crm_detail->action_name;
                $crm_detail['pic'] = !empty($crm_detail['pic']) ? json_decode($crm_detail['pic'],true) : [];

                //查找事项
                $maps = [];
                $maps[] = ['from_openid','=',$cf_detail['openid']];
                $maps[] = ['sourceid','=',$crm_detail['id']];
                $maps[] = ['source','=',1];
                $m_detail = model('matter')->get_detail($maps,'id,out_time,deal_openid');

                $crm_detail['out_time'] = !empty($m_detail) ? $m_detail['out_time'] : '';
                //$crm_detail['out_time_hi'] = !empty($m_detail) ? date('H:i',strtotime($m_detail['out_time'])) : '';
                $crm_detail['deal_openid'] = !empty($m_detail) ? $m_detail['deal_openid'] : '';

                $maps = [];
                $maps[] = ['openid','=',$crm_detail['deal_openid']];
                $u_detail = model('user')->get_detail($maps,'id,username');
                $crm_detail['deal_username'] = !empty($u_detail) ? $u_detail['username'] : '';

                //查找抄送
                $maps = [];
                $maps[] = ['from_openid','=',$cf_detail['openid']];
                $maps[] = ['sourceid','=',$crm_detail['id']];
                $maps[] = ['source','=',1];
                $copy_openid_arr = model('copy')->get_column($maps,'copy_openid');

                $crm_detail['copy_openid'] = $copy_openid_arr;

                $maps = [];
                $maps[] = ['openid','in',$copy_openid_arr];
                $copy_username_arr = model('user')->get_column($maps,'username');
                $crm_detail['copy_username'] = implode('，',$copy_username_arr);

                $temp_list[$k_2]['crm_detail'] = $crm_detail;
            }

            if($v_1['is_cp_ws'] == 1){
                //$maps = [];
                //$maps[] = ['regionid','=',$v_1['id']];
                //$wsids = model('work_station')->get_column($maps,'id');
                //$overview = $s->get_overview($wsids);

                $cp_ws['id'] = 0;
                $cp_ws['name'] = '工位使用状态';
                $cp_ws['crm_detail']['id'] = 0;
                $cp_ws['crm_detail']['cp_type'] = $cf_detail['cp_type'];
                $cp_ws['crm_detail']['cp_branchid'] = $cf_detail['cp_branchid'];
                $cp_ws['crm_detail']['cp_floorid'] = $cf_detail['id'];
                $cp_ws['crm_detail']['branchid'] = $cf_detail['branchid'];
                $cp_ws['crm_detail']['floorid'] = $cf_detail['floorid'];
                $cp_ws['crm_detail']['regionid'] = $v_1['id'];
                //$cp_ws['crm_detail']['vacant_ws_num'] = $overview['vacant_ws_num'];
                //$cp_ws['crm_detail']['person_num'] = $overview['person_num'];
                //$cp_ws['crm_detail']['use_rate'] = $overview['use_rate'];
                //$cp_ws['crm_detail']['use_ws_num'] = $overview['use_ws_num'];
                array_push($temp_list,$cp_ws);
            }

            $list[$k_1]['m_list'] = $temp_list;
        }

        $cf_detail['list'] = $list;

        $data['cf_detail'] = $cf_detail;
        return json($this->pr_logic->get_response(1,'OK',$data));
    }

    /**
     * 获取巡店工位
     * @return \think\response\Json
     */
    public function ajax_get_pr_ws(){
        $params = input();
        $r = $this->check_admin_token($params);
        if($r['code'] != 1){
            return json($r);
        }

        $pr = new \app\common\controller\PatrolRecord();

        $maps = $this->pr_logic->get_search_pr_ws_maps($params['branchid'],$params['floorid'],$params['regionid']);
        $limit = ($params['page'] - 1) * $params['rows'];
        $ws_list = model('work_station')->get_list($maps,'id,regionid,number,work_type','id desc','',$limit,$params['rows']);

        $list = [];
        foreach($ws_list as $k => $v){
            //查找区域
            $r_maps = [];
            $r_maps[] = ['id','=',$v['regionid']];
            $r_detail = model('region')->get_detail($r_maps,'id,floorid,name');
            $ws_list[$k]['region_zw'] = $r_detail['name'];

            //查找楼层
            $f_maps = [];
            $f_maps[] = ['id','=',$r_detail['floorid']];
            $f_detail = model('floor')->get_detail($f_maps,'id,branchid,name');
            $ws_list[$k]['floor_zw'] = $f_detail['name'];

            //查找分部
            $b_maps = [];
            $b_maps[] = ['id','=',$f_detail['branchid']];
            $b_detail = model('branch')->get_detail($b_maps,'id,name');
            $ws_list[$k]['branch_zw'] = $b_detail['name'];

            //获取巡店明细工位信息
            $pr_ws_info = $pr->get_pr_ws_info($params['cp_type'],$params['cp_branchid'],$params['cp_floorid'],$b_detail['id'],$f_detail['id'],$v['regionid'],$v['id']);
            $ws_list[$k]['use_status'] = $pr_ws_info['use_status'];
            $ws_list[$k]['use_status_zw'] = $pr_ws_info['use_status_zw'];
            $ws_list[$k]['use_rate'] = $pr_ws_info['use_rate'];
            $ws_list[$k]['person_num'] = $pr_ws_info['person_num'];
            $ws_list[$k]['is_holiday_work'] = $pr_ws_info['is_holiday_work'];
            $ws_list[$k]['is_holiday_work_zw'] = $pr_ws_info['is_holiday_work_zw'];
            $ws_list[$k]['is_evening_work'] = $pr_ws_info['is_evening_work'];
            $ws_list[$k]['is_evening_work_zw'] = $pr_ws_info['is_evening_work_zw'];
            $ws_list[$k]['ccids'] = $pr_ws_info['ccids'];
            $ws_list[$k]['remark'] = $pr_ws_info['remark'];
            $ws_list[$k]['status'] = $pr_ws_info['status'];
            $ws_list[$k]['status_zw'] = $pr_ws_info['status_zw'];

            $list[$k] = $v->toArray();
        }

        $total_num = model('work_station')->get_count($maps,'id');

        $data['list'] = $list;
        $data['total_num'] = $total_num;
        return json($this->pr_logic->get_response(1,'OK',$data));
    }
}